<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Intervention\Image\ImageManagerStatic as Image;//缩放
class ManageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $data = DB::table('member') 
                 -> where(function($query) use ($request){
                     $query -> where('permission','>',1) 
                     -> where('name','like','%'.$request 
                     -> input('keywords').'%');
                 })-> paginate($request -> input('num',10));
        // dd($data);
        return view('admin/member/manage/index',['data' => $data,'request' => $request  -> all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = DB::table('member') -> where('member_id',$id) -> first();
        return view('admin/member/manage/show',['data' => $data]);   
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = DB::table('member') -> where('member_id','=',$id) -> first();
        // dd($data);
        return view('admin/member/manage/edit',['data' => $data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // dd($id);
        $data = $request -> except('_token','_method','oldPic');
        // $data = $request -> all();
        // dd($data);
        $this->validate($request, [
            'nickname' => 'max:8',
        ],[
             //错误提醒
             'nickname.max' => '昵称不能超过8位',            
        ]);
        $oldPic = '.'.$request -> input('oldPic');
        // return $oldPic;
        //处理图片
        if($request ->hasFile('pic'))
        {
            if($request -> file('pic') -> isValid())
            {
                //获取后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;

                //随机文件名称
                $fileName = time().mt_rand(100000,999999).'.'.$suffix;
                
                $data['pic'] = '/uploads/member/'.$fileName;
                // dd($data);
                //将文件移入指定文件夹中
                $request -> file('pic') -> move('./uploads/member/',$fileName); 


                //等比缩放start
                $fname = './uploads/member/' . '/' . $fileName;//缩放
                $srcarr = getimagesize($fname);
                $w=200;
                $h=200;
                if($w/$srcarr[0] > $h/$srcarr[1]){
                    $bili = $h/$srcarr[1];
                }else{
                    $bili = $w/$srcarr[0];
                }
                $nw = floor($srcarr[0]*$bili);
                $nh = floor($srcarr[1]*$bili);
                $img = Image::make($fname)->resize($nw,$nh)->save($fname);//缩放
                //等比缩放end

                //更新的同时删除旧图片
                if($oldPic != '.')
                {
                    if(file_exists($oldPic))
                    {
                        unlink($oldPic);//删除旧图片
                    }
                }
               
            }

        }
        // dd($data);

        //更新数据
        $res = DB::table('member') -> where('member_id',$id) -> update($data);    
        // dd($res);
        if($res)
        {
            return redirect('/admin/manage') -> with(['success' => '更新成功']);
        }else
        {
            return back() -> with(['error' => '更新失败']);
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
